package com.uriuniv.validator;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.uriuniv.command.JoinCommand;
import com.uriuniv.command.LoginCommand;
import com.uriuniv.domain.Student;
import com.uriuniv.domain.University;
import com.uriuniv.service.StudentS;
import com.uriuniv.service.UniversityS;

public class LoginV implements Validator {
	
	private StudentS studentS;
	
	public void setStudentS(StudentS studentS) {
		this.studentS = studentS;
	}

	/**
	 * commandClass 확인
	 */
	public boolean supports(Class clazz) {
		if(LoginCommand.class.isAssignableFrom(clazz)){
			return true;
		}
		return false;
	}
	
	/**
	 * 유효성 검사
	 */
	public void validate(Object target, Errors errors) {
		LoginCommand loginCommand = (LoginCommand)target;
		
		String email = loginCommand.getEmail();
		String password = loginCommand.getPassword();
		
		Student student = studentS.getStudentByEmail(email);
		
		if(student == null){
			errors.rejectValue("email", "noExist", "해당 이메일이 존재하지 않습니다.");
		}else if(!student.getCertification().trim().equals("1")){
			errors.rejectValue("email", "noExist", "인증되지 않은 이메일입니다. 인증 완료 후 로그인하시기 바랍니다.");
		}else if(!student.getPassword().equals(password)){
			errors.rejectValue("password", "noExist", "비밀번호가 틀립니다."); 
		}
		
	}
	
}
